/**
 * Created by lx on 2017/3/6.
 */
layui.use(['form'], function () {
    var form = layui.form();
    var tagBlock = {};
    $(document).ready(function () {
        if (window.location.hash != null && window.location.hash != "") {
            var has = window.location.hash;
            has = has.replace("#", "");
            has = has.replace(/\'/g, "\"");
            tagBlock = JSON.parse(has);
            if(isNotNull(tagBlock.id)){
                $.ajax({
                    type: "POST",
                    url: "/tagBlock/listTag",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(tagBlock),
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            var content = '' ;
                            $.each(data.param.list,function(i,val){
                                content += ' <option value="'+ val.id + '">' + val.name + '</option>' ;
                            }) ;
                            $('#tags').append(content) ;
                            form.render() ;
                        } else {
                            layer.msg("获取标签列表失败");
                        }
                    },
                    error: function (message) {
                        layer.msg("获取标签列表失败");
                    }
                });
            }
        }
        autoInject(tagBlock);
        form.render();

        form.on('submit(yes)', function (data) {
            var url;
            if (tagBlock.id == undefined) {
                url = "/tagBlock/add"
            } else {
                url = "/tagBlock/update";
            }
            tagBlock = autoFetch(tagBlock);
            tagBlock.tags = [] ;
            $('#tagsInline #tags option').each(function(index,item){
                if(isNotNull(item.value)&&isNotNull(item.text)){
                    tagBlock.tags.push({id:item.value,name:item.text}) ;
                }
            }) ;
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(tagBlock),
                dataType: "json",
                success: function (data) {
                    if (data.success) {
                        layer.msg("新增标签块成功");
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    } else {
                        layer.msg("新增标签块失败");
                    }
                },
                error: function (message) {
                    layer.msg("新增标签块失败");
                }
            });
            return false;
        });

        var isAdd = false ;

        form.on('submit(addTag)', function (data) {
            isAdd = true ;
            $('#mid').show() ;
            $('#midden').show();
            $('#head').hide() ;
            $('#bottom').hide() ;
            $.ajax({
                type: "POST",
                url: "/tag/list?pageNum=1&pageSize=1000",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.success) {
                        $('#updateTag').empty() ;
                        $('#updateTag').append(createTagList(data.param)) ;
                        $('#tagsInline #tags option').each(function(index,item){
                            if(isNotNull(item.value)&&isNotNull(item.text)){
                                $('#updateTag option[value="'+item.value+'"]').remove() ;
                            }
                        }) ;
                        form.render() ;
                    } else {
                        layer.msg("获取标签列表失败");
                    }
                },
                error: function (message) {
                    layer.msg("获取标签列表失败");
                }
            });
            return false;
        });

        form.on('submit(deleteTag)', function (data) {
            $('#mid').show() ;
            $('#midden').show();
            $('#head').hide() ;
            $('#bottom').hide() ;
            $('#updateTag').empty() ;
            $('#updateTag').append($('#tagsInline').html()) ;
            form.render() ;
            return false;
        });

        form.on('submit(yesTag)', function (data) {
            $('#mid').hide() ;
            $('#midden').hide();
            $('#head').show() ;
            $('#bottom').show() ;
            if(isAdd){
                var val = $('#updateTag .layui-this').attr('lay-value') ;
                var text= $('#updateTag .layui-this').text() ;
                $('#tags').append('<option value="'+val+'">'+text+'</option>') ;
                isAdd = false ;
            }else {
                var val = $('#updateTag .layui-this').attr('lay-value') ;
                $('#tags option[value="'+val+'"]').remove() ;
            }
            form.render() ;
            return false;
        });

        form.on('submit(cancelTag)', function (data) {
            $('#mid').hide() ;
            $('#midden').hide();
            $('#head').show() ;
            $('#bottom').show() ;
            isAdd = false ;
            return false;
        });

        $('#cancel').click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
            return false;
        });
    });
})
;